var BookList = Class.create();
//Object.extend(Friends, Attention);
Object.extend(BookList, {
	//增加分组框
	message:'',
	addFrom: function(){
		new MessageBox(MessageBox.TYPE_CUSTOM, $('group_from').innerHTML, '添加书单', {ok: this.addBookList});
	},
	//增加好友分组
	addBookList: function() {
		var group_name = $F('group_name');
		if (group_name.trim() == '') {
			alert('书单名称不能为空');
			return false;
		}
		disable_form();
		new Ajax.Request('/~booklist.index.addBookList',{
			parameters: $H({title: group_name}),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.reload();
					}else{
						enable_form();
						alert(ret.reason);
					}
				} catch (e){}
			}
		});
	},
	/**
	 * 删除书单
	 */
	removeBookList:function(blid){
		if(!blid){
			new MessageBox(MessageBox.TYPE_MESSAGE, '参数错误', '提示', {close:function(){}});
			return false;
		}
		new Ajax.Request("/~booklist.index.delBookList",{
			method: 'post',
			parameters: $H({id: blid}),
			onComplete: this.complete.bindAsEventListener(this)
		});
	},
	//删除书单确认框
	delBookListFrom: function(blid) {
		var html = '<div class="friend_pw_mainr">';
		html	+= '<p>你确认要删除本书单？</p>';
		html	+= '<p>删除该书单后，书单里面的漫画绘转向默认书单。</p>';
		html	+= '</div>';	
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '删除书单', {ok: this.removeBookList.bind(this, blid)});
	},
	
	/**
	 * 删除书单中漫画
	 */
	removeBookList:function(cid,blid){
		if(!(blid && cid) ){
			new MessageBox(MessageBox.TYPE_MESSAGE, '参数错误', '提示', {close:function(){}});
			return false;
		}
		new Ajax.Request("/~booklist.index.delRel",{
			method: 'post',
			parameters: $H({cid:cid,blid: blid}),
			onComplete: this.complete.bindAsEventListener(this)
		});
	},
	confirmRemoveBookList: function(blid){
		new MessageBox(MessageBox.TYPE_YES_NO, '你确定要删除本书单吗？删除书单后，书单里面的漫画绘转向默认书单', '删除书单', {yes: this.removeBookList.bind(this, blid)});
	},
	cfmRemoveBookListRel: function(cid,blid){
		new MessageBox(MessageBox.TYPE_YES_NO, '您确定要删除书单中的漫画吗？', '删除书单漫画', {yes: this.removeBookList.bind(this, cid,blid)});
	},
	
	complete: function(transport){
		var ret = (transport.responseText).evalJSON();
		if(ret.result){
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.data?ret.data:'操作成功 ！', '提示', {close:function(){}});
			window.location.reload();
		}else{
			new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '提示', {close:function(){}});
			window.location.reload();
		}
	},
	//编辑书单框
	editGroupFrom: function(blid, gname) {
		var html = '<div style="text-align:center;">';
		html	+= '书单名称: <input type="text" id="group_name" name="group_name" value="' + gname + '" size="30">';
		html	+= '<input type="hidden" id="group_id" name="group_id" value="' + blid + '">';
		html	+= '</div>';
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '编辑书单', {ok: this.editGroup});
	},
	
	//编辑书单
	editGroup: function() {
		var group_name = $F('group_name');
		if (group_name.trim() == '') {
			alert('书单名称不能为空');
			return false;
		}
		var group_id = $F('group_id');
		
		disable_form();
		new Ajax.Request('/~booklist.index.edit',{
			parameters: $H({blid: group_id, title: group_name}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						window.location.reload();
					}else{
						enable_form();
						alert(ret.reason);
					}
				} catch (e){}
			}
		});
	},
	
	
	//把漫画从书单中移除提示框
	removeBookListRelFrom: function(cid, blid) {
		var html = '<div class="friend_pw_mainr3">';
		html	+= '<p>您确定要从当前书单中删除?</p>';
		html	+= '</div>';	
		new MessageBox(MessageBox.TYPE_CUSTOM, html, '从书单中移除漫画', {ok: this.removeBookListRel.bind(this, gid, fid)});
	},
	
	//从分组中移除
	removeBookListRel: function(cid, blid) {
		disable_form();
		new Ajax.Request("/~booklist.index.delRel",{
			method: 'post',
			parameters: $H({cid: cid, blid: blid}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();
					if(ret.result){
//						window.location.href= '/~booklist.index.list' + gid;
						window.location.reload();
					}else{
						enable_form();
						messagebox.close();
						MessageBox.showError(ret);
					}
				} catch (e){}
			}
		});
	},
	
	//添加漫画到书单框
	addCartoonToBookListFrom: function(cid){
		var self = this;
		new Ajax.Request("/~booklist.index.myBookList",{
			method: 'post',
//			parameters: $H({cid: cid, blid: blid}).toQueryString(),
			onComplete: function(transport) {
				var ret = (transport.responseText).evalJSON();
				if (ret.result) {				
					var groupdata = ret.data;
					var groupleng = groupdata.length;
					var html = '<div class="friend_pw_mainr3">';
					html	+= '请选择书单：';
					html	+= '<div class="friend_pw_group">';
	
					for (var i = 0; i < groupleng; i++) {
						var checked = '';
						if (groupdata[i].checked == 1) {
							checked = 'checked';
						}
						if (i % 4 == 0) {
							html += '<ul>';	
						}
						
						html += '<li><input type="checkbox" name="group"  ' + checked + ' value="' + groupdata[i].id + '" /> ' + groupdata[i].title + '</li>';
						
						if (((i + 1) % 4 == 0) || (i + 1 == groupleng)) {
							html += "</ul>";	
						}
					}
					
					html += '</div>';
					html += '</div>';
			
					//alert(html);
					self.message = new MessageBox(MessageBox.TYPE_CUSTOM, html, '书单', {ok: this.addCartoonToBookList.bind(this, cid)});
				} else {
					MessageBox.showError(ret);
				}
			}.bindAsEventListener(this)
		});
	},				
	
	//添加漫画到书单
	addCartoonToBookList: function(cid) {
		var groups = '';
		 $$('input[type="checkbox"][name="group"]').select(function(i){return i.checked}).each(function(i) {
		 	groups += groups == '' ? '' : ',';
		    groups += i.value;
		 });
		
		 disable_form();
		 new Ajax.Request("/~booklist.index.addRel",{
			method: 'post',
			parameters: $H({cid: cid, blid: groups}).toQueryString(),
			onComplete: function(transport){
				try{
					var ret = transport.responseText.evalJSON();					
					if(ret.result){
						$('panel').hide();
						$('panel').remove();
						if($('ieOpacityLayer')){
							$('ieOpacityLayer').remove();
						}
						new MessageBox(MessageBox.TYPE_MESSAGE,'已经成功将本漫画加入您的书单', '提示', {});
					}else{
//						MessageBox.close();
						$('panel').hide();
						$('panel').remove();
						if($('ieOpacityLayer')){
							$('ieOpacityLayer').remove();
						}
//						MessageBox.showError(ret);
						new MessageBox(MessageBox.TYPE_MESSAGE, ret.reason, '错误', {});
						enable_form();
					}
				} catch (e){}
			}
		});
	}
	
});